package xuhaijia.lanou3g.autohome.findcar.screen;
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                   \\`.
//        .'//            为什么坚持  想一想当初             \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-.  \\`.
//    .'//.-"                 `-.  |  .-'                 "-. \\`.
//  .'//______.============-..   \ | /   ..-============._______\\`.
//.'//____________________________\|/_____________________________\\`.
// 
//                             

import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import xuhaijia.lanou3g.autohome.R;
import xuhaijia.lanou3g.autohome.base.BaseFragment;
import xuhaijia.lanou3g.autohome.base.NetTool;
import xuhaijia.lanou3g.autohome.base.URLValues;
import xuhaijia.lanou3g.autohome.custom.RangeSeekBar;
import xuhaijia.lanou3g.autohome.findcar.FindCarFragment;

/**
 * Created by 7 on 16/8/23.
 */
public class ScreenFragment extends BaseFragment implements View.OnClickListener {

    public TextView conditionTv;
    private ListView screenLv;
    private ScreenAdapter adapter;
    private PopupWindow mPopWindow;
    @Override
    protected int setLayout() {
        return R.layout.fragment_screen;
    }

    @Override
    protected void initView() {
        conditionTv = bindView(R.id.screen_fragment_condition);
        conditionTv.setOnClickListener(this);
        screenLv = bindView(R.id.screen_fragment_lv);
        mPopWindow = new PopupWindow(
                WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
    }

    @Override
    protected void initData() {
        adapter = new ScreenAdapter(mContext);
        mNetTool.getNetData(URLValues.SCREENING_URL, ScreenBean.class, new NetTool.NetListener<ScreenBean>() {
            @Override
            public void onSuccess(final ScreenBean screenBean) {
                adapter.setScreenBean(screenBean);
                screenLv.setAdapter(adapter);
                screenLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent = new Intent(getContext() , ScreenInActivity.class);
                        intent.putExtra("url" , URLValues.SCREENING_IN_URL_START +
                        screenBean.getResult().getSeries().get(position).getSeriesid()
                        + URLValues.SCREENING_IN_URL_END);
                        startActivity(intent);
                    }
                });
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
    }

    @Override
    public void load() {

    }

    @Override
    public void refresh() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.screen_fragment_condition:
                if (mPopWindow != null && mPopWindow.isShowing()) {
                    conditionTv.setText("条件 ▼");
                    conditionTv.setTextColor(0xff414348);
                    closePopWindow();
                    return;
                } else {
                    showPopupWindow();
                    //显示PopupWindow
                    conditionTv.setText("条件 ▲");
                    conditionTv.setTextColor(Color.BLUE);
                    mPopWindow.setFocusable(false);
                }
                mPopWindow.showAsDropDown(v, 0, 5);
                break;
        }

    }

    private void showPopupWindow() {
        //设置contentView
        View contentView = LayoutInflater.from(mContext).inflate(R.layout.screenpop, null);
        RangeSeekBar rsb = (RangeSeekBar) contentView.findViewById(R.id.screenpop_rsb);
        rsb.setOnRangeChangeListener(new RangeSeekBar.OnRangeChangeListener() {
            @Override
            public void onRangeChange(int leftValue, int rightValue) {

                if (leftValue != 0 && rightValue == 100) {
                    conditionTv.setText(leftValue + "万以上");
                } else if (leftValue == 0 && rightValue != 100) {
                    conditionTv.setText(rightValue + "万以下");
                } else if (leftValue != 0 && rightValue != 100) {
                    conditionTv.setText(leftValue + "-" + rightValue + "万 ▲");
                } else {
                    conditionTv.setText("条件 ▲");
                }
            }
        });
        mPopWindow.setContentView(contentView);

        contentView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (mPopWindow != null && mPopWindow.isShowing()) {
                    conditionTv.setText("条件 ▼");
                    conditionTv.setTextColor(0xff414348);
                    closePopWindow();
                }
                return false;
            }
        });

    }
    public void closePopWindow() {
        mPopWindow.dismiss();
    }

}
